<%@ page import="admin.Connectmysql" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2025/6/19
  Time: 18:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    response.setCharacterEncoding("UTF-8");

    String examName = request.getParameter("examName");
    String examCourse = request.getParameter("examCourse");
    String examTeacher = request.getParameter("examTeacher");
    String examDate = request.getParameter("examDate");
    String examDuration = request.getParameter("examDuration");
    String status = request.getParameter("status");
    Connectmysql cmysql = new Connectmysql(
            "jdbc:mysql://localhost:3306/exam1",
            "root",
            "root",
            "com.mysql.cj.jdbc.Driver");
    try {
        if(cmysql.open()){

            ResultSet rst= cmysql.executeSql(
                    "SELECT name FROM exam WHERE name = ?;",
                    new Object[]{examName}
            );
            if(rst!=null&&rst.next()){
                // ID已存在，返回错误信息
                System.out.println("考试已存在");
            }else{
                ResultSet number = cmysql.executeSql("SELECT max(id) FROM exam;",new Object[]{});
                int id=1;
                if(number!=null&&number.next()){
                    id=number.getInt(1)+1;
                }
                System.out.println(id);
                int rst2 = cmysql.executeSqlUpdate("INSERT INTO exam (id,name,course, teacher, date, duration,status) VALUES (?,?, ?,?, ?, ?, ?);"
                        ,new Object[]{id ,examName,examCourse,examTeacher,examDate,examDuration,status}
                );
//                cmysql.executeSqlUpdate("CREATE TABLE ? (id INT PRIMARY KEY , text varchar(255), A varchar(255), B varchar(255), C varchar(255), D varchar(255),answer varchar(10),score int);",
//                        new Object[]{examName}
//                        );
                if(rst2>0){
                    //添加成功
                    System.out.println("添加成功");
                }else {
                    //添加失败
                }


            }
            rst.close();

        }
    } catch (ClassNotFoundException f) {
        f.printStackTrace();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }




%>
</body>
</html>
